<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;
//分类控制器
class CateController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\Cate;
	}
	//分类列表
	public function index(){
		$data=$this->model
			->join("type","type_tid","=","tid")
			->orderby("sort","ASC")
			->get();
		$data=Data::tree($data,'cname','cid','pid');
//		p($data);die;
		View::with('data',$data);
		View::make();
	}
	//添加分类
	public function add(){
		$typeModel=new \Common\Model\Type;
		$typeData=$typeModel->get();
		View::with('typeData',$typeData);
		if(IS_POST){
			if($this->model->store()){
                View::success('添加成功',U('index'));
			}
		  	// 提示错误
            View::error($this->model->getError());

		}
		View::make();
	}		
	public function del(){
		$cid=Q('get.cid',0,'intval');
		//如果分类下面没有数据，子分类上移
		//pluck直接获取当前值
		$pid=$this->model->where("cid={$cid}")->pluck('pid');
//		p($pid);die;
		$this->model->where("pid={$cid}")->update(array('pid'=>$pid));
		$this->model->where("cid={$cid}")->delete();
		View::success('删除成功',U('index'));	
	}
	//添加子类EE
	public function addSon(){
		if(IS_POST){
			if($this->model->store()){
				View::success("添加成功",U('index'));
			}
			View::error($this->model->getError());
		}
		//获取所属分类
		$cid=Q('get.cid',0,'intval');
		$data=$this->model
			 ->join("type","type_tid","=","tid")
			 ->where("cid={$cid}")
			 ->find();
//			 p($data);die;
		View::with("data",$data);
		View::make();
	}
	public function edit(){
		if(IS_POST){
			if($this->model->edit()){
				View::success("修改成功",U('index'));
			}
		View::error($this->model->getError());
			
		}
		//获得旧数据
		$cid=Q('get.cid',0,'intval');
		$oldData=$this->model
				->where("cid={$cid}")
				->find();
		$typeModel=new \Common\Model\Type;
		$typeData=$typeModel->get();
		//2.处理“所属分类”，除了自己和自己的子集
		$cateData=Data::tree($this->model->getNoMy($cid),'cname');
//		p($oldData);
//		p($typeData);
		View::with('cateData',$cateData);
		View::with('oldData',$oldData);
		View::with('typeData',$typeData);
		View::make();
	}
			
			
		
		
		
		
}
?>